<template>
  <span class="action-item"></span>
</template>
<script>
import $ from "@/common";
export default {
  name: "actionBase",
  subModuleStore: true,
  data() {
    return {
      actionSetting: {
        fullModal: true,
        modalWidth: 800
      }
    };
  },
  watch: {
    status(v) {
      if (v) {
        //状态变化调用onStatus回调方法
        var code = `on${$.upperCase(v)}`;
        if (typeof this[code] === "function") {
          this[code]();
        }
        this.$emit("statusChange", v);
      }
    }
  },
  methods: {
    //默认状态触发，用于被覆盖
    onTrigger() {
      console.info("trigger");
    },
    onCancel() {},
    //默认成功处理
    onSuccess(text) {
      this.$Message.success(text || `${this.model.name} 操作成功`);
    },
    trunState(code, value) {
      this.setState({ code, value });
    },
    //提供自带默认处理的远程调用方法，待分理处fetch与submit
    link(...arg) {
      return $.link(...arg);
    },
    submit(...arg) {
      this.actionPosting();
      return $.link(...arg)
        .then(res => {
          this.actionSuccess(res);
        })
        .catch(ex => {
          this.actionError();
          throw ex;
        });
    },
    fetch(...arg) {
      return $.link(...arg);
    }
  },
  created() {
    this.$owner = this.$parentElement.$owner;
    this.$collection = this.$parentElement.$collection;
    this.trunState("setting", this.actionSetting);
    this.pushStatus("ready");
  }
};
</script>
